﻿using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Threading.Tasks;

namespace TK.Mqtt.Client.Demo
{
    /// <summary>
    /// MyReceiveHandler
    /// </summary>
    public class MyReceiveHandler : IDataHandler
    {
        private readonly JsonSerializerOptions _options;
        private readonly ILogger<MyReceiveHandler> _logger;

        /// <summary>
        /// ctor
        /// </summary>
        public MyReceiveHandler(ILogger<MyReceiveHandler> logger)
        {
            _logger = logger;
            _options = new JsonSerializerOptions
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                UnknownTypeHandling = System.Text.Json.Serialization.JsonUnknownTypeHandling.JsonNode
            };
        }

        /// <summary>
        /// ReceiveDataAync
        /// </summary>
        public async Task ReceiveDataAync(CallbackData data)
        {
            string json = JsonSerializer.Serialize(data, _options);
            _logger.LogInformation(json);
            await Task.CompletedTask;
        }
    }
}
